package com.gec.oasys.service.impl;

import com.gec.oasys.dao.SysUserMapper;
import com.gec.oasys.pojo.SysUser;
import com.gec.oasys.service.SysUserService;
import com.gec.oasys.util.SoulPage;
import com.gec.oasys.utils.SysConstanst;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class SysUserServiceImpl implements SysUserService {
    @Autowired
    private SysUserMapper sysUserMapper;
    @Override
    public SysUser selectByPrimaryKey(Integer id) {
        return sysUserMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<SysUser> querySysUser(SysUser sysUser) {
        return sysUserMapper.querySysUser(sysUser);
    }

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return sysUserMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int deletesByPrimaryKey(String[] strId) {
        for (String strid : strId) {
            sysUserMapper.deleteByPrimaryKey(Integer.parseInt(strid));
        }
        return strId.length;
    }

    @Override
    public int insert(SysUser record){
        return sysUserMapper.insert(record);
    }
    @Override
    public int updateByPrimaryKey(SysUser record){
        return sysUserMapper.updateByPrimaryKey(record);
    }
    @Override
    public Object dataGrid(SoulPage<SysUser> soulPage) {
        return soulPage.setData(sysUserMapper.dataGrid(soulPage,(SysUser) soulPage.getObj()));
    }

    @Override
    public List<SysUser> findUserByLoginNameAndPwd(SysUser sysUser) {
        return sysUserMapper.findUserByLoginNameAndPwd(sysUser);
    }

    @Override
    public int insertEncodePassword(SysUser record) {
        String password =record.getPwd();

        String passwordMd5=new Md5Hash(password, SysConstanst.SALT,SysConstanst.HASHITERATIONS).toString();

        record.setPwd(passwordMd5);

        return insert(record);
    }
}
